package com.bms.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;

import com.bms.Logic;
import com.bms.abstracts.RESTController;
import com.bms.domain.Comment;
import com.bms.domain.Event;
import com.bms.domain.User;

public class AddCommentController extends RESTController {
	
	private Logic logic;

	public void setLogic(Logic logic) {
		this.logic = logic;
	}

	@Override
	protected ModelAndView doGet(HttpServletRequest request,
			HttpServletResponse response) {
		
		return null;
	}

	@Override
	protected ModelAndView doPost(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String eventId = request.getParameter("eventid");
		Event event = logic.getEvent(eventId);
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String createtime = logic.getCurrentDate();
		String content = request.getParameter("content");
		Comment comment = new Comment();
		comment.setEvent(event);
		comment.setUser(user);
		comment.setCreatetime(createtime);
		comment.setContent(content);
		
		int results = logic.addComment(comment);
		
		try {
			PrintWriter pw = response.getWriter();
			pw.print(results);
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
		return null;
	}

}
